//
//  ShopSortView.m
//  sensen_ios
//
//  Created by pro on 16/2/29.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopSortView.h"

@interface ShopSortView ()

@end

@implementation ShopSortView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    NSArray *titles = @[@"综合排序", @"价格从低到高", @"价格从高到低"];
    
    CGFloat x, y, width, height;
    for (int i = 0; i < titles.count; i++) {
        width = self.width;
        height = self.height / titles.count;
        x = 0;
        y = i * height;
        // 基底视图
        UIView *basalView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self addSubview:basalView];
        basalView.tag = i + 1;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [basalView addGestureRecognizer:tap];
        // 标题
        width = self.width / 2;
        x = 15;
        y = 0;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.userInteractionEnabled = YES;
        titleLabel.font = FONT(15);
        titleLabel.text = titles[i];
        titleLabel.textColor = [UIColor darkGrayColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.tag = 11;
        [basalView addSubview:titleLabel];
        
        // 选中的勾
        width = 15;
        height = 15;
        y = (basalView.height - height) / 2;
        UIImageView *selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        selectImgView.image = [UIImage imageNamed:@"选中-勾"];
        selectImgView.hidden = YES; // 开始是没选中的
        selectImgView.tag = 12;
        [basalView addSubview:selectImgView];
        selectImgView.right = self.width - 15; // 距离右边15的宽度
        
        // 线
        x = 0;
        y = 0;
        width = self.width;
        height = 0.5;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];
        [basalView addSubview:line];
        line.bottom = basalView.height;
    }
    
    
    
}


#pragma mark - 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    // 改变选中的栏的颜色
    for (UIView *basalView in self.subviews) {
        if (tap.view.tag == basalView.tag) {
            UILabel *label = (UILabel *)[basalView viewWithTag:11];
            UIView *selectImgView = [basalView viewWithTag:12];
            label.textColor = SS_NAVI_ORANGE_COLOR;
            selectImgView.hidden = NO;
        } else {
            UILabel *label = (UILabel *)[basalView viewWithTag:11];
            UIView *selectImgView = [basalView viewWithTag:12];
            label.textColor = [UIColor darkGrayColor];
            selectImgView.hidden = YES;
        }
    }
    
    
    if ([self.delegate respondsToSelector:@selector(sortViewDidChoose:)]) {
        [self.delegate sortViewDidChoose:tap.view.tag];
    }
}


#pragma mark - 其他
- (void)resetSortView {
    
    // 改变选中的栏的颜色, 隐藏选中图片
    for (UIView *basalView in self.subviews) {
        UILabel *label = (UILabel *)[basalView viewWithTag:11];
        UIView *selectImgView = [basalView viewWithTag:12];
        label.textColor = [UIColor darkGrayColor];
        selectImgView.hidden = YES;
    }
    

}

@end
